// pages/jiucanrenshu/jiucanrenshu.js
const app = getApp()
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectdate: util.formatDate(new Date()),
    currentdate: util.formatDate(new Date()),
    enddate: util.formatDate(new Date()),
    contentlist: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.queryData();
  },
  
  queryData: function() {

    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "date": that.data.selectdate,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_mealreports"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);

        if (res.data.status == 0) {
          that.setData({
            contentlist: res.data.data,
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryData();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  detailbuttton: function(e) {

    var index = e.currentTarget.dataset.index;
    var mid = this.data.contentlist[index].mid;
    var name = this.data.contentlist[index].name;
    wx.navigateTo({
      url: '../jiucanrenshuschool/jiucanrenshuschool?mid=' + mid + "&name=" + name +"&selectdate="+this.data.selectdate
    })
  },
  bindDateChange: function(e) {
    console.log(e)
    this.setData({
      selectdate: e.detail.value
    })
    this.queryData();
  },

  getDateStr: function(today, addDayCount) {
    var dd;
    if (today) {
      dd = new Date(today);
    } else {
      dd = new Date();
    }
    dd.setDate(dd.getDate() + addDayCount); //获取AddDayCount天后的日期 
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; //获取当前月份的日期 
    var d = dd.getDate();
    if (m < 10) {
      m = '0' + m;
    }
    if (d < 10) {
      d = '0' + d;
    };
    return y + "-" + m + "-" + d;
  },

  btnPreday: function() {
    var day = this.getDateStr(this.data.selectdate, -1);
    this.setData({
      selectdate: day
    })
    this.queryData();
  },

  btnNextday: function() {
    if (this.data.selectdate < this.data.currentdate) {
      var day = this.getDateStr(this.data.selectdate, 1);
      this.setData({
        selectdate: day
      })
      this.queryData();
    } else {
      wx.showToast({
        title: '还未到达明天',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    }
  },
})